package utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.NetworkOnMainThreadException;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.picture_sharing_system.MainActivity;
import com.example.picture_sharing_system.R;
import com.google.android.material.tabs.TabLayout;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WorksPictureListAdapter extends ArrayAdapter<Works> {

    private List<Works> worksData;
    private Context context;
    private int recourseId;

    public WorksPictureListAdapter(Context context, int recourseId, List<Works> worksData) {
        super(context, recourseId, worksData);
        this.context = context;
        this.recourseId = recourseId;
        this.worksData = worksData;
    }

    public void setWorksData(List<Works> worksData) {
        this.worksData = worksData;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Works works = getItem(position);
        View view;
        Log.d("TAG", "getView: " + works.getcTime());

        final ViewHolder vh;

        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(recourseId, parent, false);

            vh = new ViewHolder();
            vh.ivWorksPicture = view.findViewById(R.id.iv_works_picture);
            vh.tvWorksMsg = view.findViewById(R.id.tv_works_msg);
            vh.tvWorksCTime = view.findViewById(R.id.tv_works_cTime);
            vh.tvWorksUsername = view.findViewById(R.id.tv_works_username);
            vh.workId = view.findViewById(R.id.work_id);
            vh.ivWorksStar = view.findViewById(R.id.iv_works_star);
            vh.nowUserid = view.findViewById(R.id.now_userid);
            vh.isStarByNowUser = view.findViewById(R.id.isStarByNowUser);

            view.setTag(vh);
        } else {
            view = convertView;
            vh = (ViewHolder) view.getTag();
        }

        vh.workId.setText(works.getId());
        vh.isStarByNowUser.setText(works.getIsStarByNowUser());
        if (vh.isStarByNowUser.getText().equals("1")) {
            vh.ivWorksStar.setColorFilter(Color.parseColor("#A0E155"));
        } else {
            vh.ivWorksStar.setColorFilter(Color.parseColor("#000000"));
        }
        vh.nowUserid.setText(works.getNowUserid());
        vh.tvWorksCTime.setText(works.getcTime());
        vh.tvWorksMsg.setText(works.getMessage());
        vh.tvWorksUsername.setText(works.getUsername());
        byte[] bytes = Base64.decode(works.getPictureBase64(), Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        vh.ivWorksPicture.setImageBitmap(bitmap);

        vh.ivWorksStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "onClick: " + vh.isStarByNowUser.getText());
                if (vh.isStarByNowUser.getText().equals("0")) {
                    vh.ivWorksStar.setColorFilter(Color.parseColor("#A0E155"));
                    vh.isStarByNowUser.setText("1");
                    works.setIsStarByNowUser("1");
                    Log.d("TAG", "onClick: " + vh.isStarByNowUser.getText());


                } else {
                    vh.ivWorksStar.setColorFilter(Color.parseColor("#000000"));
                    vh.isStarByNowUser.setText("0");
                    works.setIsStarByNowUser("0");
                    Log.d("TAG", "onClick: " + vh.isStarByNowUser.getText());


                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String urlParams
                                = "?workid=" + vh.workId.getText()
                                + "&userid=" + vh.nowUserid.getText()
                                + "&flag=" + works.getIsStarByNowUser();
                        Request request = new Request.Builder()
                                .url(Constants.STAR_WORK_URL + urlParams)
                                .get().build();
                        try {
                            OkHttpClient client = new OkHttpClient();
                            client.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                                }
                            });
                        } catch (NetworkOnMainThreadException ex) {
                            ex.printStackTrace();
                        }
                    }
                }).start();
            }
        });


        return view;
    }

    class ViewHolder {
        ImageView ivWorksStar;
        TextView workId;
        TextView nowUserid;
        TextView isStarByNowUser;
        ImageView ivWorksPicture;
        TextView tvWorksMsg;
        TextView tvWorksCTime;
        TextView tvWorksUsername;
    }
}
